<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" v-if="!listLoading">
      <el-row :gutter="20">
        <el-col :span="2">
          <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加卡类</el-button>
        </el-col>
        <el-col :span="3">
          <el-select v-model="query.is_publish" @change="loadTable" placeholder="请选择">
            <el-option  label="已上线" value="1"></el-option>
            <el-option  label="待上线" value="0"></el-option>
            <el-option  label="不限制" value="2"></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="请输入关键字" v-model="query.keyword">
            <i slot="suffix" class="el-input__icon el-icon-search" @click="loadTable"></i>
          </el-input>
        </el-col>
      </el-row>

    </div>
    <el-table :data="tableData" v-loading="listLoading" border style="width: 100%">
      <el-table-column prop="title" label="卡名称" width="180"></el-table-column>
      <el-table-column prop="is_fixed_term" label="有效期" width="250" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.is_fixed_term">
            <el-tag type="gray">{{scope.row.start_date}}</el-tag>
             至
            <el-tag>{{scope.row.end_date}}</el-tag>
            <!--{{`${scope.row.start_date}  -  ${scope.row.end_date}`}}-->
          </template>
          <template v-else>
            {{ `${scope.row.range_num} ${unit[scope.row.range_unit]}` }}
          </template>
        </template>

      </el-table-column>

      <el-table-column prop="type" label="卡种类" width="180">
        <template slot-scope="scope">
          <span>{{type[scope.row.type]}}</span>
        </template>
      </el-table-column>
      <!--<el-table-column prop="start_date" label="开始日期"></el-table-column>-->
      <!--<el-table-column prop="end_date" label="结束日期"></el-table-column>-->
      <el-table-column prop="clock_start_time" label="开始时间"></el-table-column>
      <el-table-column prop="clock_end_time" label="结束时间"></el-table-column>
      <el-table-column prop="times" label="次数"></el-table-column>
      <el-table-column prop="is_publish" label="状态" width="110px" align="center">
        <template slot-scope="scope">
          <a @click="publish(scope.row.id)">
            <el-tag type='success' v-if="!scope.row.is_publish" >待上线</el-tag>
          </a>
          <el-tag type='gray' v-if="scope.row.is_publish"  >已上线</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!--<el-button type="success" :disabled="scope.row.is_publish === 1" size="small" @click="publish(scope.row.id)">{{scope.row.is_publish ? '已上线' : '可上线'}}</el-button>-->
          <el-button type="primary"  size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" v-if="!scope.row.is_publish" size="small" @click="handleDelete(scope)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <template v-if="!listLoading">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="query.pagination.total"
          :page-sizes="[5, 10, 15, 20, 30]"
          :page-size="query.pagination.per_page"
          :current-page.sync="query.pagination.current_page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </template>
    </div>
    <edit
      :dialogVisible="dialogVisible"
      :title="title"
      :editId="editId"
      @closeDialog="closeDialog"></edit>
  </div>
</template>

<script>
import edit from './edit'
import { GetTable, Delete, Publish } from '@/api/card'
export default {
  components: {
    edit
  },
  data() {
    return {
      unit: {
        day: '天',
        month: '月',
        year: '年',
        week: '周'
      },
      is_publish: ['未上线', '已上线'],
      type: {
        times: '次卡',
        date: '时间卡',
        clock: '时段卡'
      },
      select: '',
      input5: '',
      editId: null,
      listLoading: false,
      visible: [],
      delVisible: false,
      dialogVisible: false,
      title: null,
      tableData: [],
      query: {
        pagination: {
          total: 0,
          page: 1,
          per_page: 10
        },
        keyword: '',
        is_publish: '0'
      }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    handleCreate() {
      this.title = '添加'
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.title = '编辑'
      this.editId = row.id
      this.dialogVisible = true
    },
    handleDelete(rowAndColumn) {
      const id = rowAndColumn.row.id
      this.$confirm('此操作将永久删除该卡类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(id).then(() => {
          this.$message({ type: 'success', message: '删除成功!' })
          this.loadTable()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    publish(id) {
      Publish(id).then(() => {
        this.$message({
          message: '上线成功',
          type: 'success'
        })
        this.loadTable()
      })
    },
    closeDialog(loadTable) {
      this.dialogVisible = false
      if (loadTable) this.loadTable()
    },
    loadTable() {
      this.listLoading = true
      GetTable(this.query.pagination.current_page, this.query.pagination.per_page, this.query.keyword, this.query.is_publish).then((res) => {
        this.listLoading = false
        this.tableData = res.data
        this.query.pagination = res.meta.pagination
      })
    },
    handleSizeChange(val) {
      this.query.pagination.per_page = val
      this.loadTable()
    },
    handleCurrentChange(val) {
      this.query.pagination.current_page = val
      this.loadTable()
    }
  }
}
</script>
